﻿using FingerPrintCollection;
using System;
using System.Data;
using System.Data.SQLite;
using System.Threading;
using System.Windows.Forms;
using NDK.Module.DAL;
using NDK.Module.Model;
using NDK.Utils;
using BZ.Core.TaskExt;
using BZ.Logger;

namespace NDK.Frm
{
    public partial class FignerPrintForm : Form
    {
        UserFPInfoServices userFPInfoServices = new UserFPInfoServices();
        DataTable userInfo=new DataTable();
        SQLiteDataAdapter userDataAdapter = new SQLiteDataAdapter();
        Thread startRegisterFP = null;
        TipForm tipForm = new TipForm();


        #region 窗体对象实例，单例模式,保证整个工程只有一个实例
        /// <summary>
        /// 窗体对象实例，单例模式,保证整个工程只有一个实例
        /// </summary>
        /// 
        private static FignerPrintForm _instance;
        private static readonly object singletonLock = new object();
        public static FignerPrintForm Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (singletonLock)
                    {
                        if (_instance == null)
                        {
                            _instance = new FignerPrintForm();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion

        public FignerPrintForm()
        {
            InitializeComponent();
        }

        private void FignerPrintForm_Load(object sender, EventArgs e)
        {
            userInfo = SQLiteUtils.DataSourceBindToDataGridView(userInfoDGV, out userDataAdapter, Common.DatabaseName, "UserFPInfo");
            userInfo = SQLiteUtils.DataSourceBindToDataGridView(deleteUserDGV, out userDataAdapter, Common.DatabaseName, "UserFPInfo");
        }

        private void startBtn_Click(object sender, EventArgs e)
        {
            Common.FPUserInfo.UserName = "Apple";

            if (userNameTxt.Text != "")
            {
                FPFuction.FPInfo.UserName = userNameTxt.Text.Trim();

                //查询数据库是否存在该工号的指纹
                int Count_ID = userFPInfoServices.InquirySpecifyInfo2Table("UserName", FPFuction.FPInfo.UserName, "UserFPInfo");

                if (Count_ID > 3) //最多允许有3次记录
                {
                    MessageBox.Show("用户名 " + FPFuction.FPInfo.UserName + "已被注册过,请勿重新注册!!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

            }
            else
            {
                MessageBox.Show("用户名不能为空!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (authorityCb.Text.ToString().Trim() != "" && authorityCb.Text.ToString().Trim() != "---请选择---")
            {
                FPFuction.FPInfo.UserAuthority = authorityCb.Text.ToString().Trim();
            }
            else
            {
                MessageBox.Show("权限级别不能为空!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            FPFuction.b_startScanFP = false;

            startRegisterFP = TaskManager.Run(() =>
            {
                StartRegisterFP();
            });
            //startRegisterFP = new Thread(new ThreadStart(StartRegisterFP));
            //startRegisterFP.IsBackground = true;
            //startRegisterFP.Start();

            tipForm = new TipForm();
            tipForm.Show(this);

            userInfo = SQLiteUtils.DataSourceBindToDataGridView(deleteUserDGV, out userDataAdapter, Common.DatabaseName, "UserFPInfo");
            userInfo = SQLiteUtils.DataSourceBindToDataGridView(userInfoDGV, out userDataAdapter, Common.DatabaseName, "UserFPInfo");
        }

        /// <summary>
        /// 指纹录入函数
        /// </summary>
        private void StartRegisterFP()
        {
            if (!FPFuction.IsRegister)
            {
                FPFuction.IsRegister = true;
                FPFuction.RegisterCount = 0;
                FPFuction.cbRegTmp = 0;
                FPFuction.AppendUiLog.Log("开始录入指纹,请相同手指按压指纹3次...",LogType.Info);
            }
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            //DialogResult dr = MessageBox.Show("确定取消当前录入，重新开始吗?", "Tip", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            //if (dr == DialogResult.OK)
            //{
            //    int rtn = Common.fp.StopGetTemplate();

            //    if (rtn == 0)
            //    {
            //        MessageBox.Show("已取消录入指纹! ", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    }
            //    else
            //    {
            //        MessageBox.Show("指纹录入出错，请联系工程师! ErrorCode = " + rtn.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //        return;
            //    }
            //}
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        { 
            string userName = this.deleteUserDGV.SelectedCells[0].Value.ToString();
            bool deleteSuccess=userFPInfoServices.Delete(userName);
            if (deleteSuccess)
            {
                this.deleteUserDGV.Rows.Remove(this.deleteUserDGV.CurrentRow);
            }
            userInfo = SQLiteUtils.DataSourceBindToDataGridView(deleteUserDGV, out userDataAdapter, Common.DatabaseName, "UserFPInfo");
            userInfo = SQLiteUtils.DataSourceBindToDataGridView(userInfoDGV, out userDataAdapter, Common.DatabaseName, "UserFPInfo");

        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            userInfo = SQLiteUtils.DataSourceBindToDataGridView(userInfoDGV, out userDataAdapter, Common.DatabaseName, "UserFPInfo");
        }

        private void 更新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            userInfo = SQLiteUtils.DataSourceBindToDataGridView(deleteUserDGV, out userDataAdapter, Common.DatabaseName, "UserFPInfo");
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
